<?php 
    require_once('framework/msc-load.php');
    require_once('framework/session-handler.php');
    require_once('framework/msc-functions.php');

    if(isset($_POST['checkin']) && isset($_POST['checkout']) && isset($_POST['user_id']) && isset($_POST['hotel_id']) && isset($_POST['room_id']) && isset($_POST['price'])):

        global $db;

        $checkin = $_POST['checkin'];
        $checkout = $_POST['checkout'];
        $userID = $_POST['user_id'];
        $hotelID = $_POST['hotel_id'];
        $roomID = $_POST['room_id'];
        $price = $_POST['price'];

        $date1 = new DateTime($checkin);
        $date2 = new DateTime($checkout);
        $interval = $date1->diff($date2);
        $total_price = $price*$interval->days;

        $checkinTemp = explode('-',$checkin);
        $checkin = $checkinTemp[2].'/'.$checkinTemp[1].'/'.$checkinTemp[0];

        $checkoutTemp = explode('-',$checkout);
        $checkout = $checkoutTemp[2].'/'.$checkoutTemp[1].'/'.$checkoutTemp[0];

        $hotel_name = returnHotelName2($hotelID);
        $room_type = returnRoomType($roomID);

        $query = 'INSERT INTO RESERVATIONS (`DateIn`, `DateOut`, `PriceSummary`, `IDRoom`, `IDUser`)
                  VALUES ("'.$_POST['checkin'].'", "'.$_POST['checkout'].'", '.$total_price.', '.$roomID.', '.$userID.')';

        if($db->query($query) === TRUE ):
            $hasRecord = array(); // Array that contains the dates already in database for specific room
            $query = 'SELECT Date FROM AVAILABILITY WHERE `ID_Type` = '.$roomID.' AND Date BETWEEN "'.$_POST['checkin'].'" AND "'.$_POST['checkout'].'"';
            $result = $db->query($query);
            if($result):
                while ($row = $result->fetch_assoc()):
                    array_push($hasRecord, $row['Date']);
                endwhile;
            endif;
            $checkDates = getDatesFromRange($_POST['checkin'], $_POST['checkout']);
            array_pop($checkDates);// Array that contains the dates that we have to add or update. Remember! We pop the last element because this is the checkout date and we can give the room to another customer
            foreach($checkDates as $date):
                if(in_array($date, $hasRecord)){ // Check if is already in databse
                    $query = 'UPDATE AVAILABILITY 
                      SET `Reservation_Number` = (Reservation_Number + 1)
                      WHERE Date = "' . $date . '" AND ID_Type='.$roomID;
                    $db->query($query);
                }else{
                    $query = 'INSERT INTO AVAILABILITY (`Date`, `ID_Type`, `Reservation_Number`)
                  VALUES ("'.$date.'", '.$roomID.', 1)';
                    $db->query($query);
                }
            endforeach;
        endif;

    else:
        header('Location: /');
    endif;

?> 

<!DOCTYPE HTML>


<html>

<head>
    <title>MSC Hotels</title>
    
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <meta name="author" content="MSC Team">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- GOOGLE FONTS -->
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,400,300,600' rel='stylesheet' type='text/css'>
    <!-- /GOOGLE FONTS -->
    
    <link rel="stylesheet" href="assets/css/bootstrap.css">
    <link rel="stylesheet" href="assets/css/font-awesome.css">
    <link rel="stylesheet" href="assets/css/icomoon.css">
    <link rel="stylesheet" href="assets/css/styles.css">
    <script src="assets/js/modernizr.js"></script>
    
</head>

<body>

    <div class="global-wrap">
        <header id="main-header">
            <div class="header-top">
                <div class="container">
                    <div class="row">
                        <div class="col-md-3">
                            <a class="logo" href="index.php">
                                <img src="assets/img/logo-invert.png" alt="Image Alternative text" title="Image Title" />
                            </a>
                        </div>
                        <div class="col-md-5">
                            <h5 class="text-color">NOTICE: This is an experimental project.</h5>
                        </div>
                        <div class="col-md-4">
                            <div class="top-user-area clearfix">
                                <ul class="top-user-area-list list list-horizontal list-border">
                                    <?php if(isRegistered()): ?>
                                        <li class="top-user-area-avatar"><a href="user-profile.php">Hi, <?php echo NameShit(); ?></a></li>
                                    	<li><a href="/logout.php">Log Out</a></li>
                                    <?php else: ?>
                                        <li class="top-user-area-avatar"><a>Hi, <?php echo getUserName(); ?></a></li>
                                        <li><a href="/signup.php">Log In</a></li>
                                    <?php endif; ?>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="container">
                <div class="nav">
                    <ul class="slimmenu" id="slimmenu">
                        <li><a href="index.php">Home</a></li>
                        <li><a href="listings.php">Hotels</a></li>
                    </ul>
                </div>
            </div>
        </header>

        <div class="container">
            <h1 class="page-title">Booking details</h1>
        </div>

        <div class="gap"></div>

        <div class="container">
            <div class="row" data-gutter="60">
				<div class="col-md-12">
                    <table class="table table-bordered table-hover table-striped">
                        <thead>
                            <tr><th colspan="5">Review your booking details</th></tr>
                        </thead>
                        <tbody>
                            <tr><td>Hotel</td><td><?php echo $hotel_name; ?></td></tr>
                            <tr><td>RoomType</td><td><?php echo $room_type ?></td></tr>
                            <tr><td>CheckIn</td><td><?php echo $checkin; ?></td></tr>
                            <tr><td>CheckIn</td><td><?php echo $checkout; ?></td></tr>
                            <tr><td>Price</td><td><?php echo '€'.$total_price; ?></td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="gap"></div>

        <footer id="main-footer">
            <div class="container">
                <div class="row row-wrap">
                    <div class="col-md-3">
                        <a class="logo" href="index.php">
                            <img src="assets/img/logo-invert.png" alt="Image Alternative text" title="Image Title" />
                        </a>
                        <p class="mb20">Booking, reviews and advices on hotels and lots more!</p>
                        <ul class="list list-horizontal list-space">
                            <li>
                                <a class="fa fa-facebook box-icon-normal round animate-icon-bottom-to-top" href="#"></a>
                            </li>
                            <li>
                                <a class="fa fa-twitter box-icon-normal round animate-icon-bottom-to-top" href="#"></a>
                            </li>
                            <li>
                                <a class="fa fa-google-plus box-icon-normal round animate-icon-bottom-to-top" href="#"></a>
                            </li>
                            <li>
                                <a class="fa fa-linkedin box-icon-normal round animate-icon-bottom-to-top" href="#"></a>
                            </li>
                            <li>
                                <a class="fa fa-pinterest box-icon-normal round animate-icon-bottom-to-top" href="#"></a>
                            </li>
                        </ul>
                    </div>
                    <div class="col-md-4">
                        <h4>Have Questions?</h4>
                        <h5><a href="#" class="text-color">al.s.katsaros@gmail.com</a></h5>
                        <h5><a href="#" class="text-color">cskostas@gmail.com</a></h5>
                        <h5><a href="#" class="text-color">g.gravanis@gmail.com</a></h5>
                        <h5><a href="#" class="text-color">grgstoupas@gmail.com</a></h5>
                        <p>24/7 Dedicated Customer Support</p>
                    </div>
                    <div id="project-details" class="col-md-5">
                        <h4>This is an experimental project</h4>
                        <h5 class="text-color">based on "Software Engineering for Web Applications" course of "Master of Science (MSc) in Web Intelligence"</h5>
                        <a href="http://msc.it.teithe.gr/" target="_blank"><img src="assets/img/logo-swk-final-s.png" alt="" style="width:80%;" /></a>
                    </div>
                </div>
            </div>
        </footer>

        <script src="assets/js/jquery.js"></script>
        <script src="assets/js/bootstrap.js"></script>
        <script src="assets/js/slimmenu.js"></script>
        <script src="assets/js/bootstrap-datepicker.js"></script>
        <script src="assets/js/bootstrap-timepicker.js"></script>
        <script src="assets/js/nicescroll.js"></script>
        <script src="assets/js/dropit.js"></script>
        <script src="assets/js/ionrangeslider.js"></script>
        <script src="assets/js/icheck.js"></script>
        <script src="assets/js/fotorama.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        <script src="assets/js/typeahead.js"></script>
        <script src="assets/js/card-payment.js"></script>
        <script src="assets/js/magnific.js"></script>
        <script src="assets/js/owl-carousel.js"></script>
        <script src="assets/js/fitvids.js"></script>
        <script src="assets/js/tweet.js"></script>
        <script src="assets/js/countdown.js"></script>
        <script src="assets/js/gridrotator.js"></script>
        <script src="assets/js/custom.js"></script>
    </div>
</body>

</html>